package com.evon.yardmanagement.dao;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.FetchMode;
import org.hibernate.criterion.Restrictions;

import com.evon.yardmanagement.model.KeyBox;
import com.evon.yardmanagement.model.ParkingSlot;

/**
 * A data access object (DAO) providing persistence and search support for
 * KeyBox entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.abc.yardmanagement.dao.KeyBox
 * @author MyEclipse Persistence Tools
 */

public class KeyBoxDAO extends BaseHibernateDAO {
	private static final Log log = LogFactory.getLog(KeyBoxDAO.class);
	// property constants
	public static final String KEY_BOX_NUMBER = "keyBoxNumber";
	public static final String KEY_BOX_NAME = "keyBoxName";
	public static final String OCCUPIED = "occupied";
	public static final String HSC_ID = "hscId";
	public static final String HSU_ID = "hsuId";

	@Override
	public Class getQueryClass() {
		// TODO Auto-generated method stub
		return KeyBox.class;
	}

	/**
	 * Return the list of available key box of the given yard
	 * 
	 * @param yardId
	 * @return
	 */
	public List<KeyBox> getAvailableKeyBoxForYard(int yardId) {
		Criteria criteria = getSession().createCriteria(getQueryClass(), "KeyBox");
		//criteria.setFetchMode("KeyBin", FetchMode.JOIN);
		//criteria.setFetchMode("keyBin.yard", FetchMode.JOIN);
		criteria.add(Restrictions.eq("occupied", 0));
		criteria.createAlias("KeyBox.keyBin", "KeyBin").createAlias("KeyBin.yard", "Yard");
		
		criteria.add(Restrictions.eq("Yard.id", yardId));
		return criteria.list();

	}
	
	public void markKeyBoxOccupied(int keyBoxId) {
		KeyBox slot = (KeyBox)getSession().load(getQueryClass(),keyBoxId );
		slot.setOccupied(1);
		save(slot);
	}
	
	public static void main(String[] ad){
		KeyBoxDAO dao = new KeyBoxDAO();
		System.err.println("adsa-"+ dao.getAvailableKeyBoxForYard(101).size());
	}

	public List findByKeyBoxNumber(Object keyBoxNumber) {
		return findByProperty(KEY_BOX_NUMBER, keyBoxNumber);
	}

	public List findByKeyBoxName(Object keyBoxName) {
		return findByProperty(KEY_BOX_NAME, keyBoxName);
	}

	public List findByOccupied(Object occupied) {
		return findByProperty(OCCUPIED, occupied);
	}

	public List findByHscId(Object hscId) {
		return findByProperty(HSC_ID, hscId);
	}

	public List findByHsuId(Object hsuId) {
		return findByProperty(HSU_ID, hsuId);
	}

	public void markKeyBoxnUnOccupied(int keyBinId) {
		KeyBox slot = (KeyBox)getSession().load(getQueryClass(),keyBinId );
		slot.setOccupied(0);
		save(slot);
		
	}

	

}